import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jdk.nashorn.internal.parser.JSONParser;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.Security;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  Free 区块链
 * </p>
 *
 * @author KANLEI
 * @email freefree.me@qq.com
 * @create 2020-10-28
 */
@Slf4j
public class FreeChain {

    public static List<Block> blockChain = new ArrayList<Block>();
    public static HashMap<String,TransactionOutput> UTXOs = new HashMap<>(); //list of all unspent transactions.
    public static double minimumTransaction = 0.1;
    public static int difficulty = 5;
    public static Wallet walletA;
    public static Wallet walletB;
    static int difficult = 4;

    public static void main(String[] args) {
        // 添加加密算法提供商
        Security.addProvider(new BouncyCastleProvider());

    }

    /**
     * 校验区块链的合法性
     * @return
     */
    private static boolean isChainValid() {
        Block preBlock;
        Block curBlock;

        for (int i = 1; i < blockChain.size(); i++) {

            preBlock = blockChain.get(i - 1);
            curBlock = blockChain.get(i);

            if (!curBlock.hash.equalsIgnoreCase(curBlock.calculate())) {
                log.info("BlockChain is invalid");
                return false;
            }
            if (!curBlock.previousHash.equalsIgnoreCase(preBlock.hash)) {
                log.info("BlockChain is invalid");
                return false;
            }
        }
        return true;
    }
}
